package com.douya.common.ex;

import com.douya.common.enums.CommonResultEnums;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

/**
 * 说明：服务端主动抛出的异常
 *
 * @author 蒋海峰
 * @date 2020/02/01
 */
@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
public class ApiException extends RuntimeException {

    /**
     * 异常编码
     */
    Integer code;

    /**
     * 异常对象
     */
    Object data;

    public ApiException() {
        this(null, null, null);
    }

    public ApiException(String message) {
        this(null, message, null);
    }

    public ApiException(String message, Integer code) {
        this(null, message, code);
    }

    public ApiException(Object data, String message, Integer code) {
        super(message);
        this.code = code;
        this.data = data;
    }

    /**
     * 生成参数错误的ApiException
     *
     * @param message 异常信息
     * @return ApiException
     */
    public static ApiException newInvalidParams(String message) {
        return new ApiException(message, CommonResultEnums.INVALID_PARAMS.getCode());
    }

}
